package com.beamfield.common;

import com.beamfield.dao.sys.*;
import com.beamfield.dictionary.code;
import com.beamfield.model.*;
import com.beamfield.modelVo.UserAttendVo;

import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

/**
 * @author chenjj
 * @Description 初始化t_attend_day
 * @Date 2018/11/05
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
public class InitAttendDayTest {
    @Resource
    SystemDictionaryDataMapper dataMapper;
    @Resource
    PersonInfoMapper personInfoMapper;
    @Resource
    ManagerInfoMapper managerInfoMapper;
    @Resource
    AttendDayMapper attendDayMapper;
    @Resource
    LeaveMapper leaveMapper;

    @Test
    public void init(){
        //都安梁场为例
        Long beamId = 3L;

        //获取该梁场考勤所需工时
        SystemDictionaryData timeData = dataMapper.selectByCode(code.attend_time,beamId);
        //无数据字典时设置默认工时8小时
        if(timeData == null){
            timeData = new SystemDictionaryData();
            timeData.setValue("480");
        }

        //获取该梁场目前为止的所有考勤日期 table/person
        List<PersonInfo> personInfoList = personInfoMapper.selectAttendDates(beamId);
        for(PersonInfo person:personInfoList){
            String date = person.getDay();
            //获取该梁场所有的人员信息
            List<UserAttendVo> userList = managerInfoMapper.selectAllUser(beamId,null,null,null,null);
            System.out.println("总人数为"+userList.size());
            for(UserAttendVo user:userList){
                AttendDay attendDay = new AttendDay();
                attendDay.setAttend_date(CommonUtils.getDateFormat(person.getDay(),"yyyy-MM-dd"));
                attendDay.setName(user.getName());
                attendDay.setEcode(user.getEcode());

                List<PersonInfo> personInfos = personInfoMapper.selectByMinute(timeData.getValue(),user.getName(),date,beamId);
                //满足工时
                if(personInfos!=null&&personInfos.size()>0){
                    attendDay.setStatus(1);
                }
                else {
                    attendDay.setStatus(2);
                }
                //插入表
                attendDay.setOperator(1L);
                attendDay.setAddtime(new Date());
                attendDay.setUpdater(1L);
                attendDay.setUpdatetime(new Date());
                attendDay.setBeam_id(beamId);
                attendDayMapper.insertSelective(attendDay);
            }
        }
        //获取该梁场的请假出差信息 table/leave
        LeaveExample example = new LeaveExample();
        LeaveExample.Criteria criteria = example.createCriteria();
        criteria.andBeam_idEqualTo(beamId);

        List<Leave> leaveList = leaveMapper.selectByExample(example);
        for(Leave leave:leaveList){
            List<Date> dates = CommonUtils.getBetweenDates(leave.getStarttime(),leave.getEndtime());
            for(Date date:dates){
                AttendDay attendDay = attendDayMapper.getRecord(leave.getName(),CommonUtils.getTimeFormat(date,"yyyy-MM-dd"),beamId);
                if(attendDay!=null){
                    attendDay.setStatus(3);
                    attendDay.setType(leave.getReason());

                    attendDay.setUpdater(1L);
                    attendDay.setUpdatetime(new Date());
                    attendDayMapper.updateByPrimaryKey(attendDay);
                }
                else {
                    attendDay = new AttendDay();
                    attendDay.setAttend_date(date);
                    attendDay.setName(leave.getName());
                    attendDay.setEcode(leave.getEcode());
                    attendDay.setStatus(3);
                    attendDay.setType(leave.getReason());

                    attendDay.setOperator(1L);
                    attendDay.setAddtime(new Date());
                    attendDay.setUpdater(1L);
                    attendDay.setUpdatetime(new Date());
                    attendDay.setBeam_id(beamId);
                    attendDayMapper.insertSelective(attendDay);

                    System.out.println("新增");
                }
            }
        }
    }
}
